
<?php

global $g_account_dbconn;

function dal_account_get_dbconn()
{
	global $g_account_dbconn;
	
	if (isset($g_account_dbconn))
		return $g_account_dbconn;
	
	$g_account_dbconn = dal_getconn($GLOBALS['g_dal_account_dbstr']);
	return $g_account_dbconn;
}

function dal_account_register($username, $password)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$password = mysql_real_escape_string($password);
	$t_sql    = "INSERT INTO mocool_account_table(username, password) Values('$username', '$password')";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return false;
	}
	return true;
}

function dal_account_register_device($device_id)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$t_sql    = "INSERT INTO mocool_account_table(username) Values('@$device_id')";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return false;
	}
	return true;
}

function dal_account_bind_username($device_id, $username, $password)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$password = mysql_real_escape_string($password);
	$t_sql    = "UPDATE mocool_account_table SET username='$username', password='$password' WHERE username='@$device_id' and password=''";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		die(0);
	}
	return true;
}

function dal_account_get_userinfo_by_device($device_id)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$t_sql    = "SELECT user_id, username FROM mocool_account_table WHERE username='@$device_id'";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		die(0);
	}
	if (mysql_num_rows($t_result) != 1)
		return NULL;
	$row = mysql_fetch_array($t_result);
	return Array(
		'user_id' => $row['user_id'],
		'username' => $row['username']
	);
}

function dal_account_get_userinfo_by_username($username)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$t_sql    = "SELECT user_id, username FROM mocool_account_table WHERE username='$username'";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		die(0);
	}
	if (mysql_num_rows($t_result) != 1)
		return NULL;
	$row = mysql_fetch_array($t_result);
	return Array(
		'user_id' => $row['user_id'],
		'username' => $row['username']
	);
}

function dal_account_get_userinfo_by_userid($user_id)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$t_sql    = "SELECT user_id, username FROM mocool_account_table WHERE userid=$user_id";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		die(0);
	}
	if (mysql_num_rows($t_result) != 1)
		return NULL;
	$row = mysql_fetch_array($t_result);
	return Array(
		'user_id' => $row['user_id'],
		'username' => $row['username']
	);
}

function dal_account_auth($username, $password)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$password = mysql_real_escape_string($password);
	$t_sql    = "SELECT user_id FROM mocool_account_table WHERE username='$username' and password='$password'";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return -1;
	}
	
	if (mysql_num_rows($t_result) != 1)
		return -1;
	$row = mysql_fetch_array($t_result);
	return $row['user_id'];
}

function dal_account_reset_password($user_id, $password)
{
	$t_conn   = dal_account_get_dbconn($GLOBALS['g_dal_account_dbstr']);
	$password = mysql_real_escape_string($password);
	$t_sql    = "UPDATE mocool_account_table SET password='$password' WHERE user_id=$user_id";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return false;
	}
	return true;
}

?>
